<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<base href="<%=basePath%>" />
		<title>投放明细</title>
		<meta http-equiv="pragma" content="no-cache" />
		<meta http-equiv="cache-control" content="no-cache" />
		<meta http-equiv="expires" content="0" />
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
		<meta http-equiv="description" content="This is my page" />
		<link rel="stylesheet" type="text/css"
			href="<%=path%>/css/common.css" />
		<link rel="stylesheet" type="text/css" href="<%=path%>/css/tag.css" />
		<link rel="stylesheet" type="text/css" 
			href="<%=path%>/js/jquery-ui-1.8.21.custom/css/smoothness/jquery-ui-1.8.21.custom.css" />
		<script type="text/javascript" src="<%=path%>/js/jquery-1.7.2.js"></script>
		<script type="text/javascript"
			src="<%=path%>/js/jquery-ui-1.8.21.custom/js/jquery-ui-1.8.21.custom.min.js"></script>
		<script type="text/javascript" src="<%=path%>/js/form.js"></script>
		<script type="text/javascript" src="<%=path%>/js/public.js"></script>
		<script type="text/javascript">
			function setDialogWindows($element, $type) {
			   $element.dialog({
					autoOpen: true,
					modal: true,
					width: 400,
					height: 300,
					title: "物料选择",
					resizable: false,
					//show: "explode",
					//hide: "explode",
					show: "blind",
					hide: "slide",
					buttons: {
				      "确定": function(){
				      	var id_materials = "";
				      	var materialIDs = "";
				      	if($type == '2')
				      	{
				      	    $("#ajaxGetMaterialDialog li").each(function(){
					      		var id_material = $(this).attr("id");
					      		var materialID = $(this).attr("title");
					      		
					      		   $(this).children("input[type='checkbox']").each(function(){
					      			  if($(this).attr("checked")){
					      				//首个
					      				if(id_materials.length == 0){
					      					id_materials += id_material;
					      				}else{
					      					id_materials += "," + id_material;
					      				}
					      				//首个
					      				if(materialIDs.length == 0){
					      					materialIDs += materialID;
					      				}else{
					      					materialIDs += "\n" + materialID;
					      				}
					      			  }
						      	  });
					      	  });
					      	  
					      	  $("#materialID").empty();
					      	  $("#materialID").val(materialIDs);
					      	  $("#tmp_id_material").empty();
				      	      $("#tmp_id_material").val(id_materials);
				      	}
			      		else if($type == '1')
			      		{
			      		   var main_id_material = $('input:radio[name="mainMainMaterial"]:checked').parent().attr("id");
			      		   var main_text_material = $('input:radio[name="mainMainMaterial"]:checked').parent().attr("title");
			      		   
			      		   $("#mainMainMaterialID").empty();
					       $("#mainMainMaterialID").val(main_text_material);
					       $("#main_id_material").empty();
				      	   $("#main_id_material").val(main_id_material);
			      		}
			      		
						$(this).dialog("destroy");
				      },
				      "取消": function(){
				      	$(this).dialog('destroy');
				      }
				   }
				});
			}
			
			$(function() {
				$("#materialID").click(function(){
					$("#keywordMaterial").val('');
					$("#g_materialLevel").val('2');
					$.post("ajaxGetMaterial.action", {'g_materialLevel':$("#g_materialLevel").val()}, 
						function(result){
							$("#ajaxGetMaterialDialog ul").empty();
							var ajaxMaterials = result.ajaxMaterials;
							var id_materials = $("#tmp_id_material").val().split(",");
							if(ajaxMaterials == null || ajaxMaterials.length <= 0){
								alert("没有可选物料！");
							}else{
								for(i=0;i<ajaxMaterials.length;i++){
									var bool = false;
									for(j=0;j<id_materials.length;j++){
										if(ajaxMaterials[i].matId == id_materials[j]){
											bool = true;
										}
									}
									if(bool){
										$("#ajaxGetMaterialDialog ul").append("<li id=\""+ajaxMaterials[i].matId+"\" style=\"overflow:hidden;\" title=\""+ajaxMaterials[i].materialID+"\">"+"<input type=\"checkbox\" checked=\"checked\" style=\"vertical-align: middle;\" />&nbsp;"+ajaxMaterials[i].materialID+"</li>");
									}else {
										$("#ajaxGetMaterialDialog ul").append("<li id=\""+ajaxMaterials[i].matId+"\" style=\"overflow:hidden;\" title=\""+ajaxMaterials[i].materialID+"\">"+"<input type=\"checkbox\" style=\"vertical-align: middle;\" />&nbsp;"+ajaxMaterials[i].materialID+"</li>");
									}
								}
								//移入事件
								$("#ajaxGetMaterialDialog li").mouseover(function(){
									$(this).css("background-color", "#ffffaa");
								});
								//移出事件
								$("#ajaxGetMaterialDialog li").mouseout(function(){
									$(this).css("background-color", "#ffffff");
								});
								
								setDialogWindows($("#ajaxGetMaterialDialog"), $("#g_materialLevel").val());
							}
						}, "json");
					return false;
				});
				
				$("#mainMainMaterialID").click(function(){
					$("#keywordMaterial").val('');
					$("#g_materialLevel").val('1');
					$.post("ajaxGetMaterial.action", {'g_materialLevel':$("#g_materialLevel").val()}, 
						function(result){
							$("#ajaxGetMaterialDialog ul").empty();
							var ajaxMaterials = result.ajaxMaterials;
							var id_materials = $("#main_id_material").val().split(",");
							if(ajaxMaterials == null || ajaxMaterials.length <= 0){
								alert("没有可选物料！");
							}else{
								for(i=0;i<ajaxMaterials.length;i++){
									var bool = false;
									for(j=0;j<id_materials.length;j++){
										if(ajaxMaterials[i].matId == id_materials[j]){
											bool = true;
										}
									}
									if(bool){
										$("#ajaxGetMaterialDialog ul").append("<li id=\""+ajaxMaterials[i].matId+"\" style=\"overflow:hidden;\" title=\""+ajaxMaterials[i].materialID+"\">"+"<input type=\"radio\" value=\""+ajaxMaterials[i].matId+"\" checked=\"checked\" name=\"mainMainMaterial\" style=\"vertical-align: middle;\" />&nbsp;"+ajaxMaterials[i].materialID+"</li>");
									}else{
										$("#ajaxGetMaterialDialog ul").append("<li id=\""+ajaxMaterials[i].matId+"\" style=\"overflow:hidden;\" title=\""+ajaxMaterials[i].materialID+"\">"+"<input type=\"radio\" value=\""+ajaxMaterials[i].matId+"\" name=\"mainMainMaterial\" style=\"vertical-align: middle;\" />&nbsp;"+ajaxMaterials[i].materialID+"</li>");
									}
								}
								//移入事件
								$("#ajaxGetMaterialDialog li").mouseover(function(){
									$(this).css("background-color", "#ffffaa");
								});
								//移出事件
								$("#ajaxGetMaterialDialog li").mouseout(function(){
									$(this).css("background-color", "#ffffff");
								});
								
								setDialogWindows($("#ajaxGetMaterialDialog"),$("#g_materialLevel").val());
							}
						}, "json");
					return false;
				});
				
				// 物料查询
				$("#materialKeywordSearch").click(function(){
					$.post("ajaxGetMaterial.action", {'keyword':$("#keywordMaterial").val(), 'g_materialLevel':$("#g_materialLevel").val()}, 
						function(result){
							$("#ajaxGetMaterialDialog ul").empty();
							var ajaxMaterials = result.ajaxMaterials;
							var id_materials = ($("#tmp_id_material").val() + ',' + $("#main_id_material").val()).split(",");
							if(ajaxMaterials == null || ajaxMaterials.length <= 0){
								alert("没有可选物料！");
							}else{
								for(i=0;i<ajaxMaterials.length;i++){
									var bool = false;
									for(j=0;j<id_materials.length;j++){
										if(ajaxMaterials[i].matId == id_materials[j]){
											bool = true;
										}
									}
									if($("#g_materialLevel").val() == '2')
									{
									   if(bool){
										$("#ajaxGetMaterialDialog ul").append("<li id=\""+ajaxMaterials[i].matId+"\" style=\"overflow:hidden;\" title=\""+ajaxMaterials[i].materialID+"\">"+"<input type=\"checkbox\" checked=\"checked\" style=\"vertical-align: middle;\" />&nbsp;"+ajaxMaterials[i].materialID+"</li>");
										}else {
											$("#ajaxGetMaterialDialog ul").append("<li id=\""+ajaxMaterials[i].matId+"\" style=\"overflow:hidden;\" title=\""+ajaxMaterials[i].materialID+"\">"+"<input type=\"checkbox\" style=\"vertical-align: middle;\" />&nbsp;"+ajaxMaterials[i].materialID+"</li>");
										}
									}
									else if($("#g_materialLevel").val() == '1')
									{
									   if(bool){
										$("#ajaxGetMaterialDialog ul").append("<li id=\""+ajaxMaterials[i].matId+"\" style=\"overflow:hidden;\" title=\""+ajaxMaterials[i].materialID+"\">"+"<input type=\"radio\" value=\""+ajaxMaterials[i].matId+"\" checked=\"checked\" name=\"mainMainMaterial\" style=\"vertical-align: middle;\" />&nbsp;"+ajaxMaterials[i].materialID+"</li>");
										}else {
											$("#ajaxGetMaterialDialog ul").append("<li id=\""+ajaxMaterials[i].matId+"\" style=\"overflow:hidden;\" title=\""+ajaxMaterials[i].materialID+"\">"+"<input type=\"radio\" value=\""+ajaxMaterials[i].matId+"\" name=\"mainMainMaterial\" style=\"vertical-align: middle;\" />&nbsp;"+ajaxMaterials[i].materialID+"</li>");
										}
									}
								}
								//移入事件
								$("#ajaxGetMaterialDialog li").mouseover(function(){
									$(this).css("background-color", "#ffffaa");
								});
								//移出事件
								$("#ajaxGetMaterialDialog li").mouseout(function(){
									$(this).css("background-color", "#ffffff");
								});
								
								setDialogWindows($("#ajaxGetMaterialDialog"), $("#g_materialLevel").val());
							}
						}, "json");
				});
				
				// 物料查询重置
				$("#materialKeywordReset").click(function(){
					$("#keywordMaterial").val('');
					$.post("ajaxGetMaterial.action", {'g_materialLevel':$("#g_materialLevel").val()}, 
						function(result){
							$("#ajaxGetMaterialDialog ul").empty();
							var ajaxMaterials = result.ajaxMaterials;
							var id_materials = ($("#tmp_id_material").val() + ',' + $("#main_id_material").val()).split(",");
							if(ajaxMaterials == null || ajaxMaterials.length <= 0){
								alert("没有可选物料！");
							}else{
								for(i=0;i<ajaxMaterials.length;i++){
									var bool = false;
									for(j=0;j<id_materials.length;j++){
										if(ajaxMaterials[i].matId == id_materials[j]){
											bool = true;
										}
									}
									if($("#g_materialLevel").val() == '2')
									{
									   if(bool){
										$("#ajaxGetMaterialDialog ul").append("<li id=\""+ajaxMaterials[i].matId+"\" style=\"overflow:hidden;\" title=\""+ajaxMaterials[i].materialID+"\">"+"<input type=\"checkbox\" checked=\"checked\" style=\"vertical-align: middle;\" />&nbsp;"+ajaxMaterials[i].materialID+"</li>");
										}else {
											$("#ajaxGetMaterialDialog ul").append("<li id=\""+ajaxMaterials[i].matId+"\" style=\"overflow:hidden;\" title=\""+ajaxMaterials[i].materialID+"\">"+"<input type=\"checkbox\" style=\"vertical-align: middle;\" />&nbsp;"+ajaxMaterials[i].materialID+"</li>");
										}
									}
									else if($("#g_materialLevel").val() == '1')
									{
									   if(bool){
										$("#ajaxGetMaterialDialog ul").append("<li id=\""+ajaxMaterials[i].matId+"\" style=\"overflow:hidden;\" title=\""+ajaxMaterials[i].materialID+"\">"+"<input type=\"radio\" value=\""+ajaxMaterials[i].matId+"\" checked=\"checked\" name=\"mainMainMaterial\" style=\"vertical-align: middle;\" />&nbsp;"+ajaxMaterials[i].materialID+"</li>");
										}else {
											$("#ajaxGetMaterialDialog ul").append("<li id=\""+ajaxMaterials[i].matId+"\" style=\"overflow:hidden;\" title=\""+ajaxMaterials[i].materialID+"\">"+"<input type=\"radio\" value=\""+ajaxMaterials[i].matId+"\" name=\"mainMainMaterial\" style=\"vertical-align: middle;\" />&nbsp;"+ajaxMaterials[i].materialID+"</li>");
										}
									}
								}
								//移入事件
								$("#ajaxGetMaterialDialog li").mouseover(function(){
									$(this).css("background-color", "#ffffaa");
								});
								//移出事件
								$("#ajaxGetMaterialDialog li").mouseout(function(){
									$(this).css("background-color", "#ffffff");
								});
								
								setDialogWindows($("#ajaxGetMaterialDialog"), $("#g_materialLevel").val());
							}
						}, "json");
				});
				
				//
				$("#materialSubmitButton").click(function(){
					 // 删除多余层
					 $("#ajaxGetMaterialDialog ul").parent().remove();
					
					 var id_materials = $("#tmp_id_material").val();
					 var main_id_materials = $("#main_id_material").val();
					 
					 //首个
      			     if(id_materials.length == 0){
      				    id_materials += main_id_materials;
      			     }else{
      				    var arr_materials = id_materials.split(",");
      				    var flag = true;
      				    for(i=0;i<arr_materials.length;i++)
      				    {
      				       if(arr_materials[i] == main_id_materials)
      				       {
      				          flag = false;
      				          break;
      				       }
      				    }
      				    
      				    if(flag)
      				    {
      				    	if(main_id_materials.length != 0){
      				    		id_materials += "," + main_id_materials;
      				    	}
      				    }
      			     }

					 $("#id_material").val(id_materials);
					formSubmit('materialForm', 'initMaterialInfo', 'editMaterialInfo.action', showResult);
				});
				
				 //首个
   			     if($("#id_material").val().length != 0){
   				    var arr_materials = $("#id_material").val().split(",");
   				    var tmp_id_material = '';
   				    for(i=0;i<arr_materials.length;i++)
   				    {
   				       if(arr_materials[i] != $("#main_id_material").val())
   				       {
   				          if(tmp_id_material.length == 0){
	      					 tmp_id_material += arr_materials[i];
	      				  }else{
	      					 tmp_id_material += "," + arr_materials[i];
	      				  }
   				       }
   				    }
   				    
   				    $("#tmp_id_material").val(tmp_id_material);
   			     }
				
			});
		</script>
	</head>

	<body>
		<input id="main_id_material" type="hidden" readonly="readonly" value="${g_material.matId}"/>
		<input id="tmp_id_material" type="hidden" readonly="readonly"/>
		<input id="g_materialLevel" type="hidden" readonly="readonly"/>
		<form id="materialForm" method="post">
			<input name="castDetail.proMedID" type="hidden" readonly="readonly" value="${castDetail.proMedID }"/>
			<table class="table_add" style="width:100%;">
				<tr>
					<th style="width:150px;">一类物料：</th>
					<td colspan="3">
						<input id="mainMainMaterialID" type="text" class="text" style="width:225px;" readonly="readonly" value="${g_material.materialID }" />
					</td>
					<td>
						<input id="materialSubmitButton" type="button" class="btn-title" value="保存" />
					</td>
				</tr>
				<tr>
					<th style="width:150px;">延展物料：</th>
					<td colspan="3">
						<textarea id="materialID" style="width: 400px; height: 150px;" readonly="readonly">${castDetail.materialNames }</textarea>
						<input id="id_material" name="castDetail.id_material" type="hidden" readonly="readonly" value="${castDetail.id_material}" />
					</td>
				</tr>
			</table>
		</form>
		<div id="ajaxGetMaterialDialog" style="display: none;">
		  <table width="0" border="0" cellspacing="0" cellpadding="0" style="width: 100%;">
			<tr>
				<td>
					<input type="text" style="width: 250px" id="keywordMaterial"
						class="text" />
					&nbsp;
					<input id="materialKeywordSearch" type="button" class="btn-title" value="搜索" />
					&nbsp;
					<input id="materialKeywordReset" type="button" class="btn-title" value="重置" />
				</td>
			</tr>
		</table>
		<ul></ul>
		</div>
	</body>
</html>
